'''
@Author: your name
@Date: 2019-12-02 22:54:52
@LastEditTime: 2019-12-05 22:29:52
@LastEditors: 冯浩
@Description: In User Settings Edit
@FilePath: \bob_develop\fluent_python\示例5-15.py
'''


def clip(text='', max_len=80):
    '''
    在max_len前面或后面的第一个空格处截断文本
    '''
    end = None
    if len(text) > max_len:  # 文本较长时,分为从0到80,和从80开始找
        space_before = text.rfind(' ', 0, max_len)
        if space_before > 0:
            end = space_before
        else:
            space_after = text.rfind(' ', max_len)
        if space_after > 0:
            end = space_after
    if end == None:  # 未找到空格
        end = len(text)
    return text[:end].rstrip()
    '''
    返回删除了结尾空格的字符串副本。
    如果给定chars而不是None，则改为删除chars中的字符。
    '''


print(clip.__defaults__)  # ('', 80)
# <code object clip at 0x000002B9B7C16AE0, file "c:\bob_develop\fluent_python\示例5-15.py", line 9>
print(clip.__code__)
# ('text', 'max_len', 'end', 'space_before', 'space_after')
print(clip.__code__.co_varnames)
print(clip.__code__.co_argcount)  # 2


from inspect import signature
sig = signature(clip)
print(sig)
for  name , param in sig.parameters.items():
    print(param.kind,':',name, '=',param.default)

'''
(text='', max_len=80)
POSITIONAL_OR_KEYWORD : text =
POSITIONAL_OR_KEYWORD : max_len = 80
'''
